<?php if(!defined('PUBLIC_DIR')) die('No Access!');

Cnamespace('System.Web.MVC');
/// <summary>
/// 
/// </summary>
/// <var name="_Value"></var>
class ControllerBase implements iController{
	public $_Params;
	public $_View;

	/// <summary>
	/// 
	/// </summary>
	/// <returns>(void)</returns>
	public function __construct(){
		spl_autoload_unregister('AutoLoad');
		spl_autoload_register(array($this, 'autoLoader'));
		spl_autoload_register('AutoLoad');
		$this->_Value = null;
	}

	/// <summary>
	/// 
	/// </summary>
	/// <param name="name"></param>
	/// <param name="arguments"></param>
	/// <returns>(void)</returns>
	public function __call($name, $arguments) {
		if(!method_exists($this, $name)){
			$class = str_replace('Execute', 'Action', $name);
			$ob = new $class();
			if(method_exists($ob, $name))
				call_user_func_array(array(&$obj, $name), $arguments);
			else
				echo "Calling object method '$name' " . implode(', ', $arguments). "\n";
		}else
			parent::__call($name, $arguments);
  }

	/// <summary>
	/// 
	/// </summary>
	/// <param name="name"></param>
	/// <returns></returns>
	/*function __get($name){
		echo "Calling get var '$name'";
		if(isset($this->$name)){
			return $this->$name;
		}
	}*/

	/// <summary>
	/// 
	/// </summary>
	/// <param name="name">(string)Treść zdarzenia</param>
	/// <param name="_Value">(string)Temat zdarzenia</param>
	/// <returns></returns>
	/*function __set($name, $_Value){
		echo "Calling set var '$name'";
		if(isset($this->$name)){
			$this->$name = $_Value;
			return true;
		}else
			return false;
	}*/

	/// <summary>
	/// 
	/// </summary>
	/// <param name="name"></param>
	/// <returns>(bool)</returns>
	/*function __isset($name){
		echo "Calling isset var '$name'";
		if(isset($this->$name)){
			return true;
		}else
			return false;
	}

	/// <summary>
	/// 
	/// </summary>
	/// <param name="name"></param>
	/// <returns>(bool)</returns>
	public function __unset($name){
		if(isset($this->$name)){
			$this->$name = $_Value;
			return true;
		}else
			return false;
	}*/

	/// <summary>
	/// 
	/// </summary>
	/// <returns>(void)</returns>
	public function __destruct(){
		$this->PostExecute();
		spl_autoload_unregister(array($this, 'autoLoader'));
	}

	/// <summary>
	/// 
	/// </summary>
	/// <param name="_className"></param>
	/// <returns>(void)</returns>
	public function autoLoader($_className){
		$_classArray = preg_replace('/([a-z]+)([A-Z])/', '${1}_${2}', $_className);
		$_classArray = preg_split('/_/', $_classArray);
		$n = count($_classArray);
		$type = $_classArray[$n-1];

		switch($type){
			case 'Module':
			case 'Block':
			case 'Plugin':
				$name = str_replace($type, '', $_className);
				$file = 'Controler.class.php';;
				$dir = $this->GetDir() . DS . $type . 's' . DS . $name . DS;
				if(file_exists($file . $dir)){
					require_once($file . $dir);
					$name .= 'Controler';
					if(class_exists($name, false))
						eval('class ' . $_className . ' extends ' . $name . ' {}'); //class_alias($name, $_className);
				}
				break;
			case 'Model':
			case 'Action':
				$file = str_replace($type, '.class.php', $_className);
				$dir = $this->GetDir() . DS . $type . 's' . DS;
				if(file_exists($file . $dir))
					require_once($file . $dir);
				break;
		}
	}

	/// <summary>
	/// 
	/// </summary>
	/// <param name="ClassName">(string)Treść zdarzenia</param>
	/// <returns></returns>
	public function GetFile($ClassName = NULL){
		if($ClassName == NULL) $ClassName = $this;
		$ob = new ReflectionClass(get_class($ClassName));
		return $ob->getFileName();
	}

	/// <summary>
	/// 
	/// </summary>
	/// <param name="ClassName">(string)Treść zdarzenia</param>
	/// <returns></returns>
	public function GetDir($ClassName = NULL){
		if($ClassName == NULL) $ClassName = $this;
		$ob = new ReflectionClass(get_class($ClassName));
		return dirname($ob->getFileName());
	}

	/// <summary>
	/// 
	/// </summary>
	/// <returns>(void)</returns>
	public function Render(){
		$Ae = Router::GetActionFunction();
		$this->PreExecute();
		$this->_Value = $this->$Ae();
	}

	/// <summary>
	/// 
	/// </summary>
	/// <returns></returns>
	function Display(){
		$filex = $this->GetDir() . DS . 'Views' . DS . Router::GetActionName() . $this->_Value . '.html';
		$vars = get_object_vars($this);
		extract($vars);
		if(file_exists($filex)){
			return include_once($filex);
		}
		return false;
	}

	/// <summary>
	/// 
	/// </summary>
	/// <returns>(void)</returns>
	function PreExecute(){}

	/// <summary>
	/// 
	/// </summary>
	/// <returns>(void)</returns>
	function PostExecute(){}

	/// *<summary>
	/// 
	/// </summary>
	/// <returns>(void)</returns>
	function GetRequestParam($name){
		$Disp = new Dispather();
		$temp = $Disp -> GetValues();
		if(isset($temp[$name])){
			return $temp[$name];
		}
		return null;
	}
}
